//
//  TJSettingPasswController.m
//  CrossBorderPayment
//
//  Created by hc-101 on 2020/6/9.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJSettingPasswController.h"

@interface TJSettingPasswController ()


@property (weak, nonatomic) UITextField *passwordTextField;
@property (weak, nonatomic) UITextField *confirmPasswordTextField;

@end

@implementation TJSettingPasswController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.lineView.hidden = YES;
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleLabel = [UILabel lableWithSuperView:self.view font:UIFont_Px(24) color:UIColor_Hex(0x262626) title:@"设置密码".localizedString textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(36);
        make.top.offset(KNavBarHight + 46);
    }];
    
    UITextField *passwordTextField = [UITextField tj_addToView:self.view];
    passwordTextField.placeholder = @"请输入您的密码".localizedString;
    passwordTextField.secureTextEntry = YES;
    passwordTextField.font = UIFont_Px(15);
    passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    passwordTextField.keyboardType = UIKeyboardTypeASCIICapable;
    if (self.isPay) {//忘记支付密码 只能是数字
        passwordTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(36);
        make.top.equalTo(titleLabel.mas_bottom).offset(54);
        make.height.offset(34);
        make.right.offset(-36);
    }];
    
    UIView *passwordLineView = [UIView tj_addToView:self.view];
    passwordLineView.backgroundColor = UIColor_Hex(0xE5E5E5);
    [passwordLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordTextField);
        make.right.equalTo(passwordTextField);
        make.height.offset(1);
        make.top.equalTo(passwordTextField.mas_bottom).offset(2);
    }];
    
    UITextField *confirmPasswordTextField = [UITextField tj_addToView:self.view];
    confirmPasswordTextField.placeholder = @"请确认您的密码".localizedString;
    confirmPasswordTextField.secureTextEntry = YES;
    confirmPasswordTextField.font = UIFont_Px(15);
    confirmPasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    confirmPasswordTextField.keyboardType = UIKeyboardTypeASCIICapable;
    if (self.isPay) {//忘记支付密码 只能是数字
        confirmPasswordTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    [confirmPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(36);
        make.top.equalTo(passwordLineView.mas_bottom).offset(16);
        make.height.offset(34);
        make.right.offset(-36);
    }];
    
    UIView *confirmPasswordLineView = [UIView tj_addToView:self.view];
    confirmPasswordLineView.backgroundColor = UIColor_Hex(0xE5E5E5);
    [confirmPasswordLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(confirmPasswordTextField);
        make.right.equalTo(confirmPasswordTextField);
        make.height.offset(1);
        make.top.equalTo(confirmPasswordTextField.mas_bottom).offset(2);
    }];
    
    UIButton *determineButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(18) color:UIColor.whiteColor title:@"确定".localizedString];
    determineButton.backgroundColor = UIColor_Hex(0x2C6CC7);
    [determineButton shearRoundedCornersWithRadiu:22];
    [determineButton addTarget:self action:@selector(didClickNextButton) forControlEvents:UIControlEventTouchUpInside];
    [determineButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.right.offset(-36);
        make.height.offset(44);
        make.top.equalTo(confirmPasswordLineView.mas_bottom).offset(30);
    }];
    
    _passwordTextField = passwordTextField;
    _confirmPasswordTextField = confirmPasswordTextField;
    
}


- (void)didClickNextButton {
    
    [self.view endEditing:YES];
    
    NSString *password = _passwordTextField.text;
    if (!password.length) {
        [TJHud showAutoHideMessage:_passwordTextField.placeholder];
        return;
    }
    
    NSString *confirmPassword = _confirmPasswordTextField.text;
    if (!confirmPassword.length) {
        [TJHud showAutoHideMessage:_confirmPasswordTextField.placeholder];
        return;
    }
    
    if (![password isEqualToString:confirmPassword]) {
        [TJHud showAutoHideMessage:@"2次密码不一样".localizedString];
        return;
    }
    
    [self.parameters setObject:_passwordTextField.text forKey:@"password"];
    
    [self showloading];
    
    if (self.isPay) {//忘记支付密码
        [self.parameters setObject:TJUserInfo.readInfo.userId forKey:@"userId"];
        
        [TJNetworking.shareNetworking POSTNew:@"member/member/forgetPayPass" parameters:self.parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
            [self hiddenloading];
            
            [TJHud showAutoHideMessage:@"设置成功".localizedString];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSArray *pushVCAry=[self.navigationController viewControllers];
                UIViewController *popVC=[pushVCAry objectAtIndex:pushVCAry.count-3];
                [self.navigationController popToViewController:popVC animated:YES];
                
            });
            
        } failure:^(NSError * _Nonnull error) {
            [self hiddenloading];
            [TJHud showAutoHideMessage:error.localizedFailureReason];
        }];
        return;
    }
    
    
    if (_type == 1) {//注册
        
        
        [TJNetworking.shareNetworking POSTNew:@"member/member/register" parameters:self.parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
            [self hiddenloading];
            if (self.isEmailRegistered) {
                [USERDEFAULTS setObject:self.parameters[@"email"] forKey:userEmail];
            } else {
                [USERDEFAULTS setObject:self.parameters[@"phone"] forKey:userPhone];
            }
            
            [TJHud showAutoHideMessage:@"注册成功".localizedString];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
            
        } failure:^(NSError * _Nonnull error) {
            [self hiddenloading];
            [TJHud showAutoHideMessage:error.localizedFailureReason];
        }];
        return;
    }
    
    if (_type == 2) {//忘记密码
        [TJNetworking.shareNetworking POSTNew:@"member/member/forgetPass" parameters:self.parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
            [self hiddenloading];
            if (self.isEmailValidation) {
                [USERDEFAULTS setObject:self.parameters[@"phMail"] forKey:userEmail];
            } else {
                [USERDEFAULTS setObject:self.parameters[@"phMail"] forKey:userPhone];
            }
            
            [TJHud showAutoHideMessage:@"设置成功".localizedString];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
            
        } failure:^(NSError * _Nonnull error) {
            [self hiddenloading];
            [TJHud showAutoHideMessage:error.localizedFailureReason];
        }];
        return;
    }
    
}



@end
